package sourceagile.server.localFileRepository;

import java.io.File;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import sourceagile.server.doclet.featureFile.TokenizeFeatureFile;
import sourceagile.server.doclet.java.ConvertFileContentToJavaDocumentation;
import sourceagile.server.utilities.CreateTempFile;
import sourceagile.server.utilities.FileExtensions;

public class GetLocalClassFile {

	public static JSONObject jsonClassDocument(String projectName,
			String filePath) {

		String fileExtension = FileExtensions.getFileExtension(filePath);

		JSONObject classDocument = new JSONObject();

		String fileContent = ReadLocalFile.readFile(projectName, filePath);

		try {

			classDocument.put("fileContent", fileContent);

		} catch (JSONException e) {

			e.printStackTrace();
		}

		if (FileExtensions.EXTENSION_JAVA.equals(fileExtension)
				|| FileExtensions.EXTENSION_FEATURE.equals(fileExtension)) {

			classDocument = getJson(classDocument, fileContent, filePath);

		}

		return classDocument;
	}

	public static JSONObject getJson(JSONObject classDocument,
			String fileContent, String filePath) {

		String fileExtension = FileExtensions.getFileExtension(filePath);

		if (FileExtensions.EXTENSION_JAVA.equals(fileExtension)) {

			File file = CreateTempFile.get(fileContent, filePath);

			classDocument = ConvertFileContentToJavaDocumentation
					.loadClassDocumentation(classDocument, file);

		} else if (FileExtensions.EXTENSION_FEATURE.equals(fileExtension)) {

			classDocument = TokenizeFeatureFile.getJson(fileContent);
		}

		return classDocument;
	}
}
